let ZhuojiGameConstant = require("../constant/zhuojiGameConstant");
let DateUtil = require("../../../utils/dateUtil");

class JiCard {
    constructor(card, type) {
        this.card = card;
        this.type = type;
        this.count = 0;
        this.isJin = false;
    }

    setCount(count) {
        this.count = count;
        if (this.count > 1 && isChongFengZeRen(this.type)) {
            this.count = 1;
        }
    }

    getScore(playType) {
        let score = this.count;
        //这里责任鸡和吹风鸡不算分
        if(this.type === ZhuojiGameConstant.JI_TYPE_WUGU_ZEREN
            || this.type === ZhuojiGameConstant.JI_TYPE_YAOJI_ZEREN
            || this.type === ZhuojiGameConstant.JI_TYPE_CHUIFENG){
            return 0;
        }
        //如果是样样三玩法，幺鸡的冲锋鸡为2+1分，乌骨鸡的冲锋鸡为(2+1)*2分
        if(playType === ZhuojiGameConstant.PLAY_TYPE_YANG_YANG_SAN
            && (this.type === ZhuojiGameConstant.JI_TYPE_YAOJI_CHONG_FENG
                || this.type === ZhuojiGameConstant.JI_TYPE_WUGU_CHONG_FENG)){
            score ++;
        }
        if (this.type === ZhuojiGameConstant.JI_TYPE_WUGU_ZEREN ||
            this.type === ZhuojiGameConstant.JI_TYPE_WUGUJI ||
            this.type === ZhuojiGameConstant.JI_TYPE_WUGU_CHONG_FENG) {
            score = score * 2;
        }
        return score;
    }



    static createWeekJiCard() {
        let jiCards = [];
        let weekday = DateUtil.getWeekDay();
        let cardValue = weekday === 0 ? 7 : weekday;
        jiCards.push(new JiCard((cardValue | 0x00), ZhuojiGameConstant.JI_TYPE_XINGQI));
        jiCards.push(new JiCard((cardValue | 0x10), ZhuojiGameConstant.JI_TYPE_XINGQI));
        jiCards.push(new JiCard((cardValue | 0x20), ZhuojiGameConstant.JI_TYPE_XINGQI));
        return jiCards;
    }

    static createFanPaiJi(jiCard) {
        return jiCard === 0 ? null : new JiCard(this.nextCard(jiCard), ZhuojiGameConstant.JI_TYPE_FANPAI);
    }

    static createYaobaiJi(jiCard) {
        return jiCard === 0
            ?
            []
            :
            [new JiCard(this.nextCard(jiCard), ZhuojiGameConstant.JI_TYPE_YAOBAI),
                new JiCard(this.prevCard(jiCard), ZhuojiGameConstant.JI_TYPE_YAOBAI)];
    }

    static createBenJi(jiCard) {
        return new JiCard(jiCard, ZhuojiGameConstant.JI_TYPE_BENJI);
    }

    static createWuGuJi() {
        return new JiCard(0x28, ZhuojiGameConstant.JI_TYPE_WUGUJI);
    }

    static createWuGuChongFeng() {
        return new JiCard(0x28, ZhuojiGameConstant.JI_TYPE_WUGU_CHONG_FENG);
    }

    static createWuGuZeRen() {
        return new JiCard(0x28, ZhuojiGameConstant.JI_TYPE_WUGU_ZEREN);
    }

    static createYaoji() {
        return new JiCard(0x11, ZhuojiGameConstant.JI_TYPE_YAOJI);
    }

    static createChongfengJi() {
        return new JiCard(0x11, ZhuojiGameConstant.JI_TYPE_YAOJI_CHONG_FENG);
    }

    static createZeRenJi() {
        return new JiCard(0x11, ZhuojiGameConstant.JI_TYPE_YAOJI_ZEREN);
    }

    static createChuifengJi() {
        return new JiCard(0x25, ZhuojiGameConstant.JI_TYPE_CHUIFENG);
    }

    static nextCard(jiCard) {
        if (jiCard === 0) {
            return 0;
        }
        let value = jiCard & 0x0f;
        let suit = jiCard & 0xf0;
        value = value === 9 ? 1 : value + 1;
        return value | suit;
    };

    static prevCard(jiCard) {
        if (jiCard === 0) {
            return 0;
        }
        let value = jiCard & 0x0f;
        let suit = jiCard & 0xf0;
        value = value === 1 ? 9 : value - 1;
        return value | suit;
    }

};

function isChongFengZeRen(type) {
    return type === ZhuojiGameConstant.JI_TYPE_WUGU_ZEREN
        || type === ZhuojiGameConstant.JI_TYPE_YAOJI_ZEREN
        || type === ZhuojiGameConstant.JI_TYPE_YAOJI_CHONG_FENG
        || type === ZhuojiGameConstant.JI_TYPE_WUGU_CHONG_FENG;
};

module.exports = JiCard;

